package com.hieu.tripmanager;

import java.util.ArrayList;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Toast;
import com.hieu.adapter.TripAdapter;
import com.hieu.bean.Trip;
import com.hieu.dialog.AddTripDialog;
import com.hieu.dialog.AddTripDialog.OnDismissChange;
import com.hieu.quickaction.ActionItem;
import com.hieu.quickaction.QuickAction;
import com.hieu.sql.DAL;
import com.hieu.util.Util;

public class MyTripActivity extends Activity implements OnDismissChange {

	private ListView listTrip, listTrip1;
	private TripAdapter curentTripAdapter, pastTripAdapter;
	private ArrayList<Trip> mCurrentTripList, mPastTripList;
	private Button btnAdd;
	private DAL mDAL;
	private DatePickerDialog.OnDateSetListener mDateSetListener;
	private int mYear;
	private int mMonth;
	private int mDay;
	private String idTrip = null;
	private String notes = null;
	private String start = null;
	private String end = null;
	private String name = null;
	private String type = null;
	private Util util = new Util();
	private int position;
	private View headerView, headerView1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setContentView(R.layout.list_trip_layout);
		mDAL = new DAL(this);
		btnAdd = (Button) findViewById(R.id.addTrip);
		headerView = LayoutInflater.from(this).inflate(
				R.layout.layout_currenttriplist_header, null);
		headerView1 = LayoutInflater.from(this).inflate(
				R.layout.layout_pasttriplist_header, null);

		ActionItem viewAction = new ActionItem();
		viewAction.setTitle("Xem");
		viewAction.setIcon(getResources().getDrawable(R.drawable.view_quick));

		// ActionItem finishAction = new ActionItem();
		// finishAction.setTitle("Kết thúc");
		// finishAction.setIcon(getResources()
		// .getDrawable(R.drawable.finish_quick));

		ActionItem viewMapAction = new ActionItem();
		viewMapAction.setTitle("Xem bản đồ");
		viewMapAction.setIcon(getResources().getDrawable(R.drawable.map_quick));

		ActionItem deleteAction = new ActionItem();
		deleteAction.setTitle("Xóa");
		deleteAction.setIcon(getResources()
				.getDrawable(R.drawable.delete_quick));

		final QuickAction mQuickAction = new QuickAction(this);
		final QuickAction mQuickAction1 = new QuickAction(this);

		mQuickAction.addActionItem(viewAction);
		// mQuickAction.addActionItem(finishAction);
		mQuickAction.addActionItem(viewMapAction);

		mQuickAction1.addActionItem(viewAction);
		mQuickAction1.addActionItem(deleteAction);
		mQuickAction1.addActionItem(viewMapAction);

		mQuickAction
				.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					public void onItemClick(int pos) {

						if (pos == 0) {
							boolean check = util.checkContentTrip(idTrip,
									MyTripActivity.this);

							if (check) {

								Intent myIntent = new Intent(
										MyTripActivity.this,
										ViewTripActivity.class);
								myIntent.putExtra("idTrip", idTrip);
								myIntent.putExtra("notes", notes);
								myIntent.putExtra("start", start);
								myIntent.putExtra("end", end);
								myIntent.putExtra("type", type);
								myIntent.putExtra("name", name);
								startActivity(myIntent);
								// finish();
							} else {
								Toast.makeText(MyTripActivity.this,
										"Hiện tại chưa có hoạt động nào",
										Toast.LENGTH_SHORT).show();
							}

						} else if (pos == 1) {
							Intent mapIntent = new Intent(MyTripActivity.this,
									ViewMapActivity.class);
							mapIntent.putExtra("idTrip", idTrip);
							startActivity(mapIntent);
							// finish();
						}
					}
				});

		mQuickAction1
				.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
					public void onItemClick(int pos) {

						if (pos == 0) {

							boolean check = util.checkContentTrip(idTrip,
									MyTripActivity.this);

							if (check) {

								Intent myIntent = new Intent(
										MyTripActivity.this,
										ViewTripActivity.class);
								myIntent.putExtra("idTrip", idTrip);
								myIntent.putExtra("notes", notes);
								myIntent.putExtra("start", start);
								myIntent.putExtra("end", end);
								myIntent.putExtra("type", type);
								myIntent.putExtra("name", name);
								startActivity(myIntent);
								// finish();
							} else {
								Toast.makeText(MyTripActivity.this,
										"Chuyến đi không có hoạt động nào",
										Toast.LENGTH_SHORT).show();
							}

						} else if (pos == 1) {
							if (mDAL != null) {
								mDAL.deleteTrip(idTrip);
								mPastTripList.remove(position);
								pastTripAdapter.notifyDataSetChanged();
								Toast.makeText(MyTripActivity.this,
										"Xóa chuyến đi thành công",
										Toast.LENGTH_SHORT).show();
							}

						} else if (pos == 2) {
							Intent mapIntent = new Intent(MyTripActivity.this,
									ViewMapActivity.class);
							mapIntent.putExtra("idTrip", idTrip);
							startActivity(mapIntent);
							// finish();
						}
					}
				});
		finishTrip();
		mCurrentTripList = new ArrayList<Trip>();
		mPastTripList = new ArrayList<Trip>();

		if (mDAL != null) {
			mCurrentTripList = mDAL.getCurrentTripList();
			mPastTripList = mDAL.getPastTripList();

		}

		// Kiểm tra có chuyến đang thực hiện không và hiển thị
		if (mCurrentTripList != null) {

			curentTripAdapter = new TripAdapter(this, R.layout.list_trip_item,
					mCurrentTripList);
			listTrip = (ListView) findViewById(R.id.listCurrentTrip);
			listTrip.addHeaderView(headerView);
			listTrip.setAdapter(curentTripAdapter);
			listTrip.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int arg2, long arg3) {

					idTrip = null;
					idTrip = mCurrentTripList.get(arg2 - 1).getIdTrip();
					notes = mCurrentTripList.get(arg2 - 1).getNotes();
					start = mCurrentTripList.get(arg2 - 1).getStartDay();
					end = mCurrentTripList.get(arg2 - 1).getEndDay();
					type = mCurrentTripList.get(arg2 - 1).getType();
					name = mCurrentTripList.get(arg2 - 1).getNameTrip();
					mQuickAction.show(view);
					mQuickAction
							.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);

				}
			});

		}

		// Hiển thị các chuyến đi trước đây
		if (mPastTripList != null) {

			pastTripAdapter = new TripAdapter(this, R.layout.list_trip_item,
					mPastTripList);
			listTrip1 = (ListView) findViewById(R.id.listPastTrip);
			listTrip1.addHeaderView(headerView1);
			listTrip1.setAdapter(pastTripAdapter);
			listTrip1
					.setOnItemClickListener(new AdapterView.OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> arg0, View view,
								int arg2, long arg3) {

							position = arg2 - 1;
							idTrip = null;
							idTrip = mPastTripList.get(arg2 - 1).getIdTrip();
							notes = mPastTripList.get(arg2 - 1).getNotes();
							start = mPastTripList.get(arg2 - 1).getStartDay();
							end = mPastTripList.get(arg2 - 1).getEndDay();
							type = mPastTripList.get(arg2 - 1).getType();
							name = mPastTripList.get(arg2 - 1).getNameTrip();

							mQuickAction1.show(view);
							mQuickAction1
									.setAnimStyle(QuickAction.ANIM_GROW_FROM_CENTER);

						}
					});
		}

		btnAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				boolean check = util.checkIsTrip(v.getContext());
				if (check) {
					Toast.makeText(getBaseContext(),
							"Hiện tại đang có chuyến đi đang thực hiện",
							Toast.LENGTH_SHORT).show();

				} else {
					AddTripDialog t = new AddTripDialog();
					final Dialog dialog = t.createDialog(v.getContext(),
							MyTripActivity.this);
					dialog.show();

				}

			}
		});
	}

	@Override
	public void onBackPressed() {
		MyTripActivity.super.onBackPressed();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {

			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);

	}

	private void finishTrip() {
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		// Lấy ngày hiện tại của hệ thống để so sánh với ngày bắt đầu new trip
		String id = mDAL.getIdNotFinishTrip();
		if (id != null) {
			String finishDate = mDAL.getFinishDate(id);
			String currentDate = String.valueOf(new StringBuilder()
					.append(util.pad(mDay)).append("/")
					.append(util.pad(mMonth + 1)).append("/")
					.append(util.pad(mYear)));
			
			if (finishDate.equals(currentDate) || util.checkEndDate(finishDate, currentDate))
				mDAL.updateFinishTrip(id);
		}
	}

	// // Show Date Time in TextView
	// private StringBuilder updateDisplay() {
	// StringBuilder st = new StringBuilder().append(util.pad(mDay))
	// .append("/").append(util.pad(mMonth + 1)).append("/")
	// .append(util.pad(mYear));
	// return st;
	// }

	// Create DateTime Dialog
	@Override
	protected Dialog onCreateDialog(int id) {
		return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
	}

	public void onChange() {
		onResume();
	}

}
